package com.hyc.myvideo.data.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.base.BaseListAdapter;
import com.hyc.myvideo.data.api.model.HomeCourse;
import com.hyc.myvideo.utils.imguitl.ImgUitl;
import java.util.List;

/**
 * 推荐课程Adapter
 * Created by dfeads on 2017/8/2.
 */

public class HomeClassAdapter extends BaseListAdapter<HomeCourse> {

  public HomeClassAdapter(Context context, List<HomeCourse> objects) {
    super(context, objects);
  }

  @NonNull @Override
  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    HomeClassHolder holder;
    if (convertView == null) {
      if (getItemViewType(position) == 0) {
        holder = new HomeClassHolder();
        convertView = inflater.inflate(R.layout.adapter_itme_no, parent, false);
        holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.r_no_id);
      } else {
        convertView = inflater.inflate(R.layout.item_column, parent, false);
        holder = new HomeClassHolder(convertView);
      }
      convertView.setTag(holder);
    } else {
      holder = (HomeClassHolder) convertView.getTag();
    }

    if (hasNoData) {
      AbsListView.LayoutParams lp =
          new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight() * 2 / 3);
      holder.noDataRootLayout.setLayoutParams(lp);
    } else {
      HomeCourse homeCourse = mDataList.get(position);

      ImgUitl.setImg(homeCourse.getCover(), holder.mImg);

      holder.mText.setText(homeCourse.getDescribe());
      holder.mTime.setText(homeCourse.getCurriculum_people_count() + "人已练习");
      //0=免费，1=vip,2=收费
      switch (homeCourse.getCurriculum_charge_state()) {
        case 0:
          holder.mTvTip.setVisibility(View.GONE);
          break;
        case 1:
          holder.mTvTip.setText("VIP");
          holder.mTvTip.setBackgroundResource(R.drawable.item_home_tip_vip);
          holder.mTvTip.setVisibility(View.VISIBLE);
          break;
        case 2:
          holder.mTvTip.setText("付费");
          holder.mTvTip.setBackgroundResource(R.drawable.item_home_tip_pay);
          holder.mTvTip.setVisibility(View.VISIBLE);
          break;
      }
      //1系列 2单个
      if (homeCourse.getState() == 2) {
        //1=视频,2=音频,3=图文
        switch (homeCourse.getCurriculum_hour_type()) {
          case 1:
            holder.mTip.setImageResource(R.mipmap.home_video);
            break;
          case 2:
            holder.mTip.setImageResource(R.mipmap.home_audio);
            break;
          case 3:
            holder.mTip.setImageResource(R.mipmap.home_img_text);
            break;
        }
        holder.mTitle.setText(homeCourse.getTitle());
        holder.mTip.setVisibility(View.VISIBLE);
      } else {
        holder.mTitle.setText(
            Html.fromHtml(homeCourse.getTitle() + "<font color='#00D5E2'> · 系列</font>"));
        holder.mTip.setVisibility(View.GONE);
      }
    }
    return convertView;
  }

  class HomeClassHolder {
    public HomeClassHolder() {
    }

    public HomeClassHolder(View view) {
      ButterKnife.bind(this, view);
    }

    LinearLayout noDataRootLayout;
    /** 封面 */
    @BindView(R.id.item_column_img) ImageView mImg;
    /** 音频/视频/图文 标志 */
    @BindView(R.id.item_column_img_tip) ImageView mTip;
    /** 标题 */
    @BindView(R.id.item_column_title) TextView mTitle;
    /** 简介 */
    @BindView(R.id.item_column_text) TextView mText;
    /** 练习过的人数 */
    @BindView(R.id.item_column_time) TextView mTime;
    /** 免费/VIP/付费 标志 */
    @BindView(R.id.item_column_tv_tip) TextView mTvTip;
  }
}
